<?php
namespace Modules\ApiV2\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Modules\Service\Logic\AddressLogic;
use Modules\Token\Facades\Token;

class AddressController extends BaseController{

    /**
     * 获取全国城市
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function chinaCity()
    {
        $result = (new AddressLogic())->getCityList();
        return self::success($result);

    }

    //新增收货地址
    public function addressAdd(Request $request){
        $userId = Token::data("id");
        $username = $request->input("username",null);
        $mobile = $request->input("mobile",null);
        $province = $request->input("province",null);
        $city = $request->input("city",null);
        $district = $request->input("district",null);
        $address = $request->input("address",null);
        $postalCode = $request->input("postal_code");
        $selected = $request->input("selected",0);
        if (empty($username)){
            return self::fail(1003,"'username'未赋值");
        }
        if (empty($mobile)){
            return self::fail(1003,"'mobile'未赋值");
        }
        if (empty($province)){
            return self::fail(1003,"'province'未赋值");
        }
        if (empty($city)){
            return self::fail(1003,"'city'未赋值");
        }
        if (empty($district)){
            return self::fail(1003,"'district'未赋值");
        }
        if (empty($address)){
            return self::fail(1003,"'address'未赋值");
        }
        return AddressLogic::addressAdd($userId,$username,$mobile,$province,$district,$city,$address,$postalCode,$selected);
    }

    //编辑地址
    public function addressEdit(Request $request){
        $id = $request->input("id",null);
        $username = $request->input("username",null);
        $mobile = $request->input("mobile",null);
        $province = $request->input("province",null);
        $city = $request->input("city",null);
        $district = $request->input("district",null);
        $address = $request->input("address",null);
        $postalCode = $request->input("postal_code",null);
        $selected = $request->input("selected",null);
        if (empty($id)){
            return self::fail(1003,"'id'未赋值");
        }
        return AddressLogic::addressEdit($id,$username,$mobile,$province,$city,$district,$address,$postalCode,$selected);
    }

    //删除地址
    public function addressDel(Request $request){
        //接收参数
        if (!User::find(Token::data('id')))return self::fail(1014,"用户不存在");
        $id = $request->input("id",false);
        if (!isset($id))return self::fail(1003,"缺少必要参数:id");
        return AddressLogic::addressDel($id);
    }


    //查询默认地址
    public function addressSelected(){
        //获取用户信息
        $userId = Token::data('id');
        $userData = User::find($userId);
        if (!$userData)return self::fail(1014,"用户不存在");
        return AddressLogic::addressSelectFind($userId);
    }

    //收货地址列表
    public function addressList(){
        //获取用户信息
        $userId = Token::data('id');
        $userData = User::find($userId);
        if (!$userData)return self::fail(1014,"用户不存在");
        return AddressLogic::addressUserAll($userId);
    }
}